package gameObject.resources3D
{
	import assets.Asset;
	
	import away3d.animators.SkeletonAnimationSet;
	import away3d.animators.SkeletonAnimator;
	import away3d.animators.data.Skeleton;
	import away3d.core.base.Geometry;
	import away3d.entities.Mesh;
	import away3d.materials.ColorMaterial;
	
	import events.LoadedEvent;
	
	import flash.events.Event;
	
	import tool.MatTool;
	import tool.DataParser;
	
	public class GameMesh extends Mesh
	{
		public static const LOD_LOW:String = "low";
		public static const LOD_HIGH:String = "hig";
		
		public static const TYPE_STATIC:String = "static";
		public static const TYPE_SKINNED:String = "skinned";
		
		private var _meshURL:String;
		private var _skeletonURL:String;
		private var _meshData:XML;
		private var _skeletonData:XML;
		
		private var _type:String;
		private var _meshs:Array;
		private var _gotAssets:uint=0;
		private var _allAssets:uint=0;
		
		private var _animationNames:Array;
		////////////////////////////////////////////////////////////

		public function get animationNames():Array
		{
			return _animationNames;
		}

		public function get type():String
		{
			return _type;
		}
		
		public function GameMesh(url:String,MeshData:XML=null,SkeletonData:XML=null)
		{
			//trace("gameMesh:",url);
			var geo:Geometry = new Geometry;
			var mat:ColorMaterial = new ColorMaterial;
			mat.alpha = 0;
			super(geo, mat);
			
			_meshURL = url;
			_meshData = MeshData;
			_skeletonData = SkeletonData;
			
			var strArr:Array = _meshURL.split(/\.|\_/);
			var format:String = strArr[strArr.length-2];
			
			if(format=="skm")_type = TYPE_SKINNED
			else if(format=="mes")_type = TYPE_STATIC;
			
			if(SkeletonData)_type = TYPE_SKINNED;
			
			switch(_type)
			{
				case TYPE_STATIC:
					if(Asset.resStaticMeshs[_meshURL]==undefined && _meshData)setupMesh();
					break;
				case TYPE_SKINNED:
					if(Asset.resSkinnedMeshs[_meshURL]==undefined && _meshData)setupMesh();
					break;	
				default:
					break;
			}
		}

		public function load():void
		{
			if(_type == TYPE_SKINNED)
			{
				Asset.mesh.addEventListener(LoadedEvent.SKINMESH_COMPLETE,skinnedMeshLoaded);
				Asset.mesh.loadSkinedMesh(_meshURL);
			}else if(_type == TYPE_STATIC)
			{
				Asset.mesh.addEventListener(LoadedEvent.STATICMESH_COMPLETE,staticMeshLoaded);
				Asset.mesh.loadStaticMesh(_meshURL);
			}else
			{
				throw new Error("格式不支持");
			}
		}
		
		////////////////////////////////////////////////////////////
		private function setupMesh():void
		{
			if(_type == TYPE_STATIC)Asset.resStaticMeshs[_meshURL] = DataParser.getStaticMeshFromData(_meshData)
			else if(_type == TYPE_SKINNED)
			{
				var skeletonURL:String = Global.assetPath+_meshData.@skeleton;
				var meshData:Object = DataParser.getSkinedMeshFromData(_meshData,_skeletonData);
				
				Asset.resSkinnedMeshs[_meshURL] = meshData;
				Asset.resSkeletons[skeletonURL] =meshData.skeleton;
			}
		}
		
		private function staticMeshLoaded(e:LoadedEvent):void
		{
			if(e.URL==_meshURL)
			{
				Asset.mesh.removeEventListener(LoadedEvent.STATICMESH_COMPLETE,staticMeshLoaded);
				
				_gotAssets = 0;
				
				var cot:int = -1;
				_meshs = Asset.resStaticMeshs[e.URL].geometrys;
				
				_allAssets = _meshs.length*2;
				
				for each(var vals:Object in _meshs)
				{
					cot++;
					geometry.addSubGeometry(vals.subGeo);
					MatTool.setMaterial(subMeshes[cot],vals.mat,assetComplete);
					
					if(vals.fixalpha=="true")
					{
						cot++;
						geometry.addSubGeometry(vals.subGeo);
						
						var matXml:XML = new XML(vals.mat.toString());
						matXml.@simpleAlpha = "true";
						
						MatTool.setMaterial(subMeshes[cot],matXml);
					}
					
				}
				
				var cs:Boolean = Asset.resStaticMeshs[e.URL].castShadow;
				castsShadows = cs;
				
				assetComplete();
			}
		}
		
		private function skinnedMeshLoaded(e:LoadedEvent):void
		{
			if(e.URL==_meshURL)
			{
				//trace(_meshURL,"loaded");
				Asset.mesh.removeEventListener(LoadedEvent.SKINMESH_COMPLETE,skinnedMeshLoaded);
				
				_gotAssets = 0;
				
				_meshs = Asset.resSkinnedMeshs[e.URL].geometrys;
				
				_allAssets = _meshs.length*2+1;
				
				_skeletonURL = Asset.resSkinnedMeshs[e.URL].skeletonURL;
				Asset.skeleton.addEventListener(LoadedEvent.SKELETON_COMPLETE,SkeletonLoaded);
				Asset.skeleton.load(_skeletonURL);
				
				var cot:int = -1;
				for each(var vals:Object in _meshs)
				{
					cot++;
					geometry.addSubGeometry(vals.subGeo);
					MatTool.setMaterial(subMeshes[cot],vals.mat,assetComplete);
					
					if(vals.fixalpha=="true")
					{
						cot++;
						geometry.addSubGeometry(vals.subGeo);
						
						var matXml:XML = new XML(vals.mat.toString());
						matXml.@simpleAlpha = "true";
						
						MatTool.setMaterial(subMeshes[cot],matXml);
					}
					assetComplete();
				}
				
				var cs:Boolean = Asset.resSkinnedMeshs[e.URL].castShadow;
				castsShadows = cs;
				
			}
		}
		
		private function SkeletonLoaded(e:LoadedEvent):void
		{
			if(e.URL==_skeletonURL)
			{
				//trace(_skeletonURL,"loaded");
				
				Asset.skeleton.removeEventListener(LoadedEvent.SKELETON_COMPLETE,SkeletonLoaded);
				
				var _skeleton:Skeleton = Asset.resSkeletons[e.URL].skeleton;
				var _animationSet:SkeletonAnimationSet = Asset.resSkeletons[e.URL].animationSet;
				
				animator = new SkeletonAnimator(_animationSet,_skeleton);
				
				_animationNames = new Array;
				for each(var sq:String in _animationSet.animationNames)_animationNames.push(sq);
				
				assetComplete();
			}
		}
		
		/**
		 *当模型与材质都下载与设置完成时，则发送完成事件通和外部 
		 */		
		private function assetComplete():void
		{
			_gotAssets++;
			
			if(_gotAssets == _allAssets)
			{
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
	}
}